package com.lanyam.travel.base.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lanyam.travel.base.entity.TravelMenu;
import com.lanyam.travel.base.service.ITravelMenuService;
import com.lanyam.travel.base.utils.JSONResult;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author rosson
 * @since 2020-02-24
 */
@RestController
@RequestMapping("/menu")
public class TravelMenuController {
	
	@Autowired
	private ITravelMenuService travelMenuService;
	
	/**
	 * 获取所有父级菜单
	 * @return
	 */
	@GetMapping("/getFatherMenu")
	public JSONResult getFatherMenu()
	{
		List<TravelMenu> allFatherMenu = travelMenuService.getAllFatherMenu();
		return JSONResult.ok(allFatherMenu);
	}
	/**
	 * 添加菜单
	 * @param menu
	 * @return
	 */
	@PostMapping("/addMenu")
	public JSONResult addMenu(@RequestBody TravelMenu menu)
	{
		if(StringUtils.isEmpty(menu.getMenuName())||StringUtils.isEmpty(menu.getUrl()))
		{
			return JSONResult.errorMsg("参数不全");
		}
		String fatherId = menu.getFatherMenuId();
		int index = 0;
		if(StringUtils.isEmpty(fatherId))
		{
			index = travelMenuService.getFatherMenuMaxIndex();
		}
		else
		{
			index = travelMenuService.getChildMenuMaxIndex(fatherId);
		}
		String id = UUID.randomUUID().toString().replace("-", "");
		menu.setId(id);
		index++;
		menu.setIndexNum(index);
		travelMenuService.addTravleMenu(menu);
		return JSONResult.ok();
	}
	
	/**
	 * 修改菜单信息
	 * @param menu
	 * @return
	 */
	@PostMapping("/updateMenu")
	public JSONResult updateMenu(@RequestBody TravelMenu menu)
	{
		String fatherId = menu.getFatherMenuId();
		int index = 0;
		if(StringUtils.isEmpty(fatherId))
		{
			index = travelMenuService.getFatherMenuMaxIndex();
		}
		else
		{
			index = travelMenuService.getChildMenuMaxIndex(fatherId);
		}
		if(index == menu.getIndexNum())
		{
			return JSONResult.errorMsg("该位置的菜单已存在，请修改后重试");
		}
		travelMenuService.updateMenu(menu);
		return JSONResult.ok();
	}
	
	/**
	 * 获取某个菜单详情
	 * @param id
	 * @return
	 */
	@PostMapping("/getMenuById")
	public JSONResult getMenuById(String id)
	{
		if(StringUtils.isEmpty(id))
		{
			return JSONResult.errorMsg("参数不全");
		}
		TravelMenu menu = travelMenuService.getMenuById(id);
		return JSONResult.ok(menu);
	}
	
	/**
	 * 删除菜单
	 * @param id
	 * @return
	 */
	@PostMapping("/deleteMenuById")
	public JSONResult deleteMenuById(String id)
	{
		if(StringUtils.isEmpty(id))
		{
			return JSONResult.errorMsg("参数不全");
		}
		TravelMenu menu = travelMenuService.getMenuById(id);
		String fatherId = menu.getFatherMenuId();
		//如果fatherId是空，说明自己是父菜单，需要删除自己的同时，删除里面的子菜单
		if(StringUtils.isEmpty(fatherId))
		{
			travelMenuService.deleteByFatherId(fatherId);
		}
		travelMenuService.deleteById(id);
		return JSONResult.ok();
	}
	
	/**
	 * 获取所有菜单
	 * @return
	 */
	@GetMapping("/getAllMenu")
	public JSONResult getAllMenu()
	{
		List<TravelMenu> allMenu = travelMenuService.getAllMenu();
		return JSONResult.ok(allMenu);
	}
	
	/**
	 * 搜索菜单
	 * @param name
	 * @return
	 */
	@PostMapping("/searchMenu")
	public JSONResult searchMenu(String name)
	{
		List<TravelMenu> searchMenu = travelMenuService.searchMenu(name);
		return JSONResult.ok(searchMenu);
	}
}

